package com.example.carbonbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.carbonbackend.constant.CommonConstant;
//import com.example.carbonbackend.model.dto.ListingInforing.companyQueryRequest;
import com.example.carbonbackend.model.dto.company.CompanyQueryRequest;
import com.example.carbonbackend.model.entity.Company;
import com.example.carbonbackend.model.entity.ListingInfo;
import com.example.carbonbackend.model.vo.CompanyVO;
import com.example.carbonbackend.service.CompanyService;
import com.example.carbonbackend.mapper.CompanyMapper;
import com.example.carbonbackend.utils.SqlUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 25103
* @description 针对表【company(公司信息表)】的数据库操作Service实现
* @createDate 2024-03-30 15:20:01
*/
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company>
    implements CompanyService{



    /**
     * 根据查询条件构建 QueryWrapper 对象，用于分页查询公司信息。
     *
     * @param companyQueryRequest 包含查询条件的请求对象
     * @return 查询条件构建完成的 QueryWrapper 对象
     */
    @Override
    public QueryWrapper<Company> getCompanyPage(CompanyQueryRequest companyQueryRequest) {
        QueryWrapper<Company> queryWrapper = new QueryWrapper<>();
        if (companyQueryRequest == null) {
            return queryWrapper;
        }

        Long id = companyQueryRequest.getId();
        String companyName = companyQueryRequest.getCompanyName();
        String address = companyQueryRequest.getAddress();
        String type = companyQueryRequest.getType();
        Date createTime = companyQueryRequest.getCreateTime();
        Date updateTime = companyQueryRequest.getUpdateTime();
        Integer isDelete = companyQueryRequest.getIsDelete();
        String companyFile = companyQueryRequest.getCompanyFile();
        String phoneNumber = companyQueryRequest.getPhoneNumber();
        String email = companyQueryRequest.getEmail();

        // 拼接查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id)
                .eq(ObjectUtils.isNotEmpty(companyName), "company_name", companyName)
                .eq(ObjectUtils.isNotEmpty(address), "address", address)
                .eq(ObjectUtils.isNotEmpty(type), "type", type)
                .eq(ObjectUtils.isNotEmpty(createTime), "create_time", createTime)
                .eq(ObjectUtils.isNotEmpty(updateTime), "update_time", updateTime)
                .eq(ObjectUtils.isNotEmpty(isDelete), "is_delete", isDelete)
                .eq(ObjectUtils.isNotEmpty(companyFile), "company_file", companyFile)
                .eq(ObjectUtils.isNotEmpty(phoneNumber), "phone_number", phoneNumber)
                .eq(ObjectUtils.isNotEmpty(email), "email", email);

        // 判断排序字段和排序顺序是否为空，如果不为空，则添加排序条件
        if (StringUtils.isNotBlank(companyQueryRequest.getSortField()) && StringUtils.isNotBlank(companyQueryRequest.getSortOrder())) {
            if (CommonConstant.SORT_ORDER_ASC.equals(companyQueryRequest.getSortOrder())) {
                queryWrapper.orderByAsc(companyQueryRequest.getSortField());
            } else if (CommonConstant.SORT_ORDER_DESC.equals(companyQueryRequest.getSortOrder())) {
                queryWrapper.orderByDesc(companyQueryRequest.getSortField());
            }
        }

        return queryWrapper;
    }

    /**
     * 将公司信息的分页查询结果转换为视图对象的分页查询结果。
     *
     * @param companyPage 公司信息的分页查询结果
     * @param request HTTP 请求对象
     * @return 视图对象的分页查询结果
     */
    @Override
    public Page<CompanyVO> convertToPageVO(Page<Company> companyPage, HttpServletRequest request) {
        List<Company> companyList = companyPage.getRecords(); // 获取分页中的数据列表
        Page<CompanyVO> companyVOPage = new Page<>(companyPage.getCurrent(), companyPage.getSize(), companyPage.getTotal()); // 创建新的分页对象

        if (CollectionUtils.isEmpty(companyList)) {
            return companyVOPage;
        }

        List<CompanyVO> collect = companyList.stream().map(company -> {
            CompanyVO companyVO = new CompanyVO();
            BeanUtils.copyProperties(company, companyVO);
            return companyVO;
        }).collect(Collectors.toList());

        companyVOPage.setRecords(collect); // 设置分页中的数据列表

        return companyVOPage;
    }


}




